import {FormControl, FormGroup} from "@angular/forms";
import {Observable} from "rxjs/Observable";

// 手机号校验器
export function mobileValidator(control:FormControl): any {
  var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
  let valid = myreg.test(control.value);
  console.log("手机号的校验结果是："+valid);
  return valid ? null : {mobile: true};
}

// 异步校验器
// 手机号校验器
export function mobileAsyncValidator(control:FormControl): any {
  var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
  let valid = myreg.test(control.value);
  console.log("手机号的校验结果是："+valid);
  return Observable.of(valid ? null : {mobile: true}).delay(5000);
}

// 确定密码
export function equalValidator(group: FormGroup) : any {
  let password:FormControl = group.get("password") as FormControl;
  let pconfig:FormControl = group.get("pconfig") as FormControl;
  let valid: boolean = password.value === pconfig.value;
  console.log("密码校验结果："+valid);
  return valid ? null : {equal: {desc:"密码和确认密码不匹配哦!"}};
}
